package com.jysz.certificate.web.action.system;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.model.core.SystemPreferences;
import com.jysz.certificate.service.system.SystemPreferencesService;


@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/system/preferences")
public class SystemPreferencesAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger log = LoggerFactory.getLogger(SystemPreferencesAction.class);

	private SystemPreferences entity;
	
	@Resource
	private SystemPreferencesService systemPreferencesService;
	
//	private List<SystemPreferences> systemPreferences = new ArrayList<SystemPreferences>(); 
	
	
	@Action(CREATE)
	public String create(){
		try {
			if(entity != null && entity.getConfigValue()!= null){
				SystemPreferences systemPreferences = systemPreferencesService.findConfigByConfigType(entity.getConfigTypeKey());
				if(systemPreferences != null){
					setFailPopMessage("配置文件出错。");
				}else{
					if(StringUtils.isNotBlank(entity.getConfigValue())){
						entity.setConfigValue("{" + entity.getConfigValue() +"}");
					}
					systemPreferencesService.save(entity);
				}
			}
			setSuccessPopMessage("系统参数成功!");
		} catch (Exception e) {
			setFailPopMessage("系统参数异常!");
		}
		setRedirectUrl("edit.do?entity.configTypeKey=" + entity.getConfigTypeKey());
		return SHOW_MSG_PAGE;
	}
	
	@Action(UPDATE)
	public String update(){
		try {
			if(entity != null && StringUtils.isNotBlank(entity.getConfigValue())){
				entity.setConfigValue("{" + entity.getConfigValue() +"}");
				if(entity.getId() != null){
					systemPreferencesService.save(entity);
				}
			}
			setSuccessPopMessage("系统参数修改成功!");
		} catch (Exception e) {
			setFailPopMessage("系统参数修改异常!");
		}
		setRedirectUrl("edit.do?entity.configTypeKey=" + entity.getConfigTypeKey());
		return SHOW_MSG_PAGE;
	}
	
	@Action(EDIT)
	public String edit(){
//		ConfigTypes.PERFORMANCE_CONFIG.getKey()
		String appHtml =  systemPreferencesService.getSystemPreferencesHtml(entity.getConfigTypeKey());
		setJsonText(appHtml.toString());
		SystemPreferences systemPreferences = systemPreferencesService.findConfigByConfigType(entity.getConfigTypeKey());
		entity  = systemPreferences != null ? systemPreferences : entity;
		log.debug("进入系统设置。。");
		return EDIT;
	}

	public SystemPreferences getEntity() {
		return entity;
	}

	public void setEntity(SystemPreferences entity) {
		this.entity = entity;
	}

	
}
